package b2.widget
{
	import b2.net.*;
	import b2.player.*;
	import b2.scene.*;
	import b2.scene.base.*;
	import b2.scene.camp.*;
	import b2.scene.guide.*;
	import b2.util.*;
	import b2.widget.friend.FriendPane;
	import b2.widget.rank.*;
	import b2.widget.tank.*;
	import b2.widget.task.*;
	import b2.widget.tech.*;
	import b2.widget.warehouse.*;
	
	import flash.display.Sprite;
	import flash.events.*;

	public class BottomBar extends Sprite
	{
		public var warehouseIcon:BasicIcon;
		
		public function BottomBar()
		{
			super();
			//背景
			var BottomBarBgr:Class = ResourceHelper.getInstance().getClassByName("BottomBar");
			var bottomBarBgr:Sprite = new BottomBarBgr();
			bottomBarBgr.x = 200;
			bottomBarBgr.y = BFont.stageHeight - 50;
			addChild(bottomBarBgr);
			//仓库
			var WarehouseIcon:Class = ResourceHelper.getInstance().getClassByName("WarehouseIcon");
			warehouseIcon = new BasicIcon(WarehouseIcon, "仓库", 0, true);
			warehouseIcon.x = bottomBarBgr.x + 120;
			warehouseIcon.y = BFont.stageHeight - 70;
			addChild(warehouseIcon);
			warehouseIcon.addEventListener(MouseEvent.CLICK, onWarehouseIconClick);
			//科技
			var TechIcon:Class = ResourceHelper.getInstance().getClassByName("TechIcon");
			var techIcon:BasicIcon = new BasicIcon(TechIcon, "科技", 0, true);
			techIcon.x = bottomBarBgr.x + 190;
			techIcon.y = BFont.stageHeight - 70;
			addChild(techIcon);
			techIcon.addEventListener(MouseEvent.CLICK, onTechIconClick);
			//任务
			var TaskIcon:Class = ResourceHelper.getInstance().getClassByName("TaskIcon");
			var taskIcon:BasicIcon = new BasicIcon(TaskIcon, "任务", 0, true);
			taskIcon.x = bottomBarBgr.x + 260;
			taskIcon.y = BFont.stageHeight - 70;
			addChild(taskIcon);
			taskIcon.addEventListener(MouseEvent.CLICK, onTaskIconClick);
			//荣誉
			var HonourIcon:Class = ResourceHelper.getInstance().getClassByName("HonourIcon");
			var honourIcon:BasicIcon = new BasicIcon(HonourIcon, "荣誉", 0, true);
			honourIcon.x = bottomBarBgr.x + 330;
			honourIcon.y = BFont.stageHeight - 70;
			addChild(honourIcon);
			honourIcon.addEventListener(MouseEvent.CLICK, onHonourIconClick);
			//好友
			var FriendIcon:Class = ResourceHelper.getInstance().getClassByName("FriendIcon");
			var friendIcon:BasicIcon = new BasicIcon(FriendIcon, "好友", 0, true);
			friendIcon.x = bottomBarBgr.x + 400;
			friendIcon.y = BFont.stageHeight - 70;
			addChild(friendIcon);
			friendIcon.addEventListener(MouseEvent.CLICK, onFriendIconClick);
		}
		
		public static function onWarehouseIconClick(event:MouseEvent):void
		{
			showWarehouse();
		}
		
		public static function showWarehouse(startGuideFlag:Boolean = false):void
		{
			ServerHelper.getInstance().getPackageAndTank();
			ServerHelper.getInstance().addEventListener(ServerEvent.GET_PACKAGE_AND_TANK, onGetAll);
			//弹出坦克界面和仓库
			var tankDialog:TankDialog = TankDialog.getInstance();
			tankDialog.x = 50;
			tankDialog.y = 100;
			var warehouse:Warehouse = Warehouse.getInstance();
			warehouse.x = 396;
			warehouse.y = 100; 
			Warehouse.getInstance().selectTab(1);
			WidgetManager.getInstance().popOut([warehouse, tankDialog], true);
			if (startGuideFlag)
				tankDialog.startGuide();
		}
		
		private static function onGetAll(event:ServerEvent):void
		{
			var me:PlayerInfo = PlayerInfoManager.getInstance().players[PlayerInfoManager.getInstance().myUserId];
			TankDialog.getInstance().fresh(me.campTankId);
			Warehouse.getInstance().refresh();
			GuideScene.getInstance().step++;
		}
		
		public static function showTechTower(techIndex:int, warehouseIndex:int, startGuideFlag:Boolean = false):void
		{
			var techTower:TechTower = TechTower.getInstance();
			techTower.selectTab(techIndex);
			techTower.x = 45;
			techTower.y = 40;
			var warehouse:Warehouse = Warehouse.getInstance();
			warehouse.selectTab(warehouseIndex);
			warehouse.refresh();
			warehouse.x = 396;
			warehouse.y = 100; 
			WidgetManager.getInstance().popOut([warehouse, techTower], true);
			if (startGuideFlag)
				techTower.startGuide();
		}
		
		public static function onTechIconClick(event:MouseEvent):void
		{
			showTechTower(0, 1);
		}
		
		public static function showTaskDialog(startGuideFlag:Boolean = false):void
		{
			//弹出任务窗口
			var taskDialog:TaskDialog = TaskDialog.getInstance();
			taskDialog.x = (BFont.stageWidth - taskDialog.width) / 2;
			taskDialog.y = 40;
			WidgetManager.getInstance().popOut([taskDialog], true);
			if (startGuideFlag)
				taskDialog.startGuide();
			if (!ServerHelper.getInstance().getTask())
				taskDialog.refresh();
			else			
				ServerHelper.getInstance().addEventListener(ServerEvent.GET_TASK, onGetTaskComplete);
		}

		public static function onTaskIconClick(event:MouseEvent):void
		{
			showTaskDialog();
		}
		
		private static function onGetTaskComplete(event:ServerEvent):void
		{
			TaskDialog.getInstance().refresh();
		}

		public static function onHonourIconClick(event:MouseEvent):void
		{
			ServerHelper.getInstance().getRankingInfo();
			ServerHelper.getInstance().addEventListener(ServerEvent.GET_RANKING, onGetRankingComplete);
			//弹出荣誉堂
			var rankingDialog:RankingDialog = RankingDialog.getInstance();
			rankingDialog.x = (BFont.stageWidth - rankingDialog.width) / 2;
			rankingDialog.y = 60;
			WidgetManager.getInstance().popOut([rankingDialog], true);
		}
		
		private static function onGetRankingComplete(event:ServerEvent):void
		{
			RankingDialog.getInstance().refresh(event.userData as RankingPackage);
		}

		public static function onFriendIconClick(event:MouseEvent):void
		{
			var friendPane:FriendPane = FriendPane.getInstance();
			friendPane.x = BFont.stageWidth - friendPane.width;
			friendPane.y = 80;
			friendPane.refresh();
			WidgetManager.getInstance().popOut([friendPane]);
		}
	}
}